Skip to content

Conversation

@ccpu
Copy link

@ccpu ccpu commented Mar 14, 2025

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

The WebSocketCtor is defined at module scope which captures WebSocket reference before mocking libraries like MSW can intercept it. Since bundlers (webpack/vite) typically load engine.io-client before MSW, the original WebSocket implementation is used even when mocks are applied later.

New behavior

WebSocketCtor reference is moved inside the createSocket method, ensuring it always uses the current value of globalThis.WebSocket at call time. This allows mocking libraries to properly intercept WebSocket connections in Socket.IO.

Other information (e.g. related issues)

Fixes mswjs/socket.io-binding#12
I've tested this solution with both webpack and vite, confirming it resolves the issue in both bundlers.

@ccpu ccpu closed this by deleting the head repository May 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MSW intercepts plain WebSocket but fails to intercept Socket.IO connections

1 participant